package cn.xiaoyu.su.exception;

import cn.xiaoyu.su.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author sufs
 * @date 2022/10/27 11:33
 */
@ControllerAdvice
@Slf4j
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData handle(Exception e) {

        if (e instanceof BizException) {

            log.error("【业务异常】：{}",e);
            BizException bizException = (BizException) e;

            return JsonData.buildCodeAndMsg(bizException.getCode(),bizException.getMessage());
        } else {

            log.error("【非业务异常】:{}",e);
            return JsonData.buildError("【系统异常，未知错误】");
        }

    }
}
